package com.asktgapp.user;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.asktgapp.R;
import com.asktgapp.application.MyApplication;
import com.asktgapp.modulebase.common.util.Util;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.beta.download.DownloadListener;
import com.tencent.bugly.beta.download.DownloadTask;

/**
 * 自定义Activity.
 */
public class UpgradeActivity extends Activity {
    private TextView tv;
    private TextView title;
    private TextView version;
    private TextView size;
    private TextView time;
    private TextView content;
    private TextView cancel;
    private TextView start;
    private TextView type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_upgrade);
        setFinishOnTouchOutside(false);
        tv = getView(R.id.tv);
        title = getView(R.id.title);
        version = getView(R.id.version);
        size = getView(R.id.size);
        time = getView(R.id.time);
        content = getView(R.id.content);
        cancel = getView(R.id.cancel);
        start = getView(R.id.start);
        type = getView(R.id.type);
        type.setVisibility(View.GONE);
        updateBtn(Beta.getStrategyTask());
        tv.setText(tv.getText().toString() + Beta.getStrategyTask().getSavedLength() + "");
        title.setText(title.getText().toString() + Beta.getUpgradeInfo().title);
        version.setText(version.getText().toString() + Beta.getUpgradeInfo().versionName);
        size.setText(size.getText().toString() +sizeFormatNum2String(Beta.getUpgradeInfo().fileSize));
        time.setText(time.getText().toString() + Util.getFullDate(Beta.getUpgradeInfo().publishTime));
        content.setText(Beta.getUpgradeInfo().newFeature);
        if (Beta.getUpgradeInfo().updateType == 2)
            type.setText(type.getText().toString() + "强制更新");
        else
            type.setText(type.getText().toString() + "建议更新");
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DownloadTask task = Beta.startDownload();
                updateBtn(task);
                tv.setVisibility(View.VISIBLE);
//                if (task.getStatus() == DownloadTask.DOWNLOADING) {
//                    finish();
//                }
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Beta.getUpgradeInfo().upgradeType == 2) {
                    finish();
                    MyApplication.getInstance().removeALLActivity();
                } else {
                    Beta.cancelDownload();
                    finish();
                }
            }
        });
        Beta.registerDownloadListener(new DownloadListener() {
            @Override
            public void onReceive(DownloadTask task) {
                updateBtn(task);
                tv.setVisibility(View.VISIBLE);
                tv.setText("下载进度:"+sizeFormatNum2String(task.getSavedLength() ));
            }

            @Override
            public void onCompleted(DownloadTask task) {
                updateBtn(task);
                tv.setVisibility(View.VISIBLE);
                tv.setText("下载进度:"+sizeFormatNum2String(task.getSavedLength() ));
            }

            @Override
            public void onFailed(DownloadTask task, int code, String extMsg) {
                updateBtn(task);
                tv.setText("下载进度:完成");

            }
        });
    }
    public double pers = 1048576; //1024*1024
    //long==> 616.19KB,3.73M
    public  String sizeFormatNum2String(long size) {
        String s = "";
        if(size>1024*1024)
            s=String.format("%.2f", (double)size/pers)+"M";
        else
            s=String.format("%.2f", (double)size/(1024))+"KB";
        return s;
    }
    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Beta.unregisterDownloadListener();
    }


    public void updateBtn(DownloadTask task) {
        switch (task.getStatus()) {
            case DownloadTask.INIT:
            case DownloadTask.DELETED:
            case DownloadTask.FAILED: {
                start.setText("开始下载");
            }
            break;
            case DownloadTask.COMPLETE: {
                start.setText("安装");
            }
            break;
            case DownloadTask.DOWNLOADING: {
                start.setText("暂停");
            }
            break;
            case DownloadTask.PAUSED: {
                start.setText("继续下载");
            }
            break;
            default:
                break;
        }
    }

    public <T extends View> T getView(int id) {
        return (T) findViewById(id);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
